package com.jeady.timetodo.base

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.jeady.jxcompose.SliderInput
import com.jeady.timetodo.ui.theme.TimeToDoTheme

@Composable
fun BaseContainer(content: @Composable BoxScope.()->Unit){
    TimeToDoTheme {
        Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center){
            content()
        }
    }
}

@Composable
fun TimeSlider(modifier: Modifier=Modifier, onTimeSettingChange:(Long)->Unit){
    val rangeSlider = 0f..120f
    var timeSetting by remember { mutableLongStateOf(rangeSlider.start.toLong() * 1000) }
    Row(
        Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        var expandedUniit by remember { mutableStateOf(false) }
        var selectedUnit by remember { mutableStateOf("秒") }
        var selectedValue by remember { mutableFloatStateOf(0f) }
        Text("倒计时")
        SliderInput(Modifier.weight(1f), range = 0f..120f) {
            selectedValue = it
        }
        LaunchedEffect(selectedValue, selectedUnit) {
            timeSetting = selectedValue.toLong() * (if (selectedUnit == "分") 60 else 1) * (if (selectedUnit == "时") 3600 else 1) * 1000
            onTimeSettingChange(timeSetting)
        }
        Box {
            TextButton({
                expandedUniit = !expandedUniit
            }) {
                Text(selectedUnit)
            }
            DropdownMenu(
                expandedUniit,
                onDismissRequest = { expandedUniit = false }) {
                DropdownMenuItem({
                    Text("秒")
                }, onClick = { selectedUnit = "秒"; expandedUniit = false })
                DropdownMenuItem({
                    Text("分钟")
                }, onClick = { selectedUnit = "分"; expandedUniit = false })
                DropdownMenuItem({
                    Text("小时")
                }, onClick = { selectedUnit = "时"; expandedUniit = false })
            }
        }
    }
}

@Composable
fun Title(text: String, subTitle: String, modifier: Modifier=Modifier, color: Color= Color(0xffbbbbbb)) {
    Column(modifier, horizontalAlignment = Alignment.CenterHorizontally) {
        if (text.isNotBlank()) {
            Text(
                text,
                color = color,
                fontWeight = FontWeight.SemiBold,
                fontSize = 30.sp
            )
        }
        if (subTitle.isNotBlank()) {
            val offsetColor = Color(0x00101010)
            val subColor = color.subtract(offsetColor)
            Text(
                subTitle,
                color = subColor,
                fontSize = 20.sp
            )
        }
    }
}